/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023.
 * Description: support lock MSR_IA32_FEAT_CTL register for x86
 * Author: Abbott Liu <liuwenliang@huawei.com>
 * Create: 2023-09-14
 */

#ifdef CONFIG_RTOS_HAL_LOCK_IA32_FEAT_CTL
#include <asm/cmdline.h>
#include <linux/types.h>
#include <asm/msr-index.h>
#include <linux/printk.h>
#include <asm/msr.h>

static bool lock_ia32_feat_ctl_flag;
static int __init lock_ia32_feat_ctl_setup(char *__unused)
{
	lock_ia32_feat_ctl_flag = true;
	return 1;
}

__setup("lock_ia32_feat_ctl", lock_ia32_feat_ctl_setup);
void lock_ia32_feat_ctl(void)
{
	u64 msr;
	if (!lock_ia32_feat_ctl_flag)
		return;
	if (rdmsrl_safe(MSR_IA32_FEAT_CTL, &msr)) {
		pr_err_once("read MSR_IA32_FEAT_CTL failed\n");
		return;
	}
	/* lock MSR_IA32_FEAT_CTL to avoid initializing by kernel */
	if (msr & FEAT_CTL_LOCKED)
		return;
	msr |= FEAT_CTL_LOCKED;
	wrmsrl(MSR_IA32_FEAT_CTL, msr);
}

#endif
